#!/bin/bash

# Simple All-in-One Release Script
# Executes complete release workflow in one command

set -e

SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
PROJECT_ROOT="$(dirname "$SCRIPT_DIR")"

# Colors
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
BLUE='\033[0;34m'
NC='\033[0m'

# Configuration
VERSION="${1:-v1.0.0}"
MODEL="${2:-yolov8n}"
INSTALL_PATH="${3:-/opt/goyolo}"

# Helper functions
print_header() {
    echo -e "\n${BLUE}=== $1 ===${NC}\n"
}

print_success() {
    echo -e "${GREEN}✓ $1${NC}"
}

print_step() {
    echo -e "${YELLOW}→ $1${NC}"
}

# Main workflow
main() {
    print_header "GoYOLO Release Workflow"
    
    # Step 1: Create release package
    print_step "Creating release package ($VERSION)..."
    bash "$SCRIPT_DIR/create-release.sh" "$VERSION"
    print_success "Release package created"
    
    # Step 2: Download models
    print_step "Downloading and converting models ($MODEL)..."
    bash "$SCRIPT_DIR/download-models.sh" "$PROJECT_ROOT/release/goyolopenvino-$VERSION/models" "$MODEL"
    print_success "Models downloaded and converted"
    
    # Step 3: Package release
    print_step "Packaging release archive..."
    cd "$PROJECT_ROOT/release"
    tar czf "goyolopenvino-$VERSION.tar.gz" "goyolopenvino-$VERSION"
    sha256sum "goyolopenvino-$VERSION.tar.gz" > "goyolopenvino-$VERSION.sha256"
    cd - > /dev/null
    print_success "Release archive created"
    
    # Step 4: Verify release
    print_step "Verifying release archive..."
    cd "$PROJECT_ROOT/release"
    sha256sum -c "goyolopenvino-$VERSION.sha256"
    cd - > /dev/null
    print_success "Release archive verified"
    
    # Step 5: Deploy release
    print_step "Deploying release to $INSTALL_PATH..."
    bash "$SCRIPT_DIR/release-manager.sh" deploy \
        "$PROJECT_ROOT/release/goyolopenvino-$VERSION.tar.gz" \
        "$INSTALL_PATH"
    print_success "Release deployed"
    
    # Summary
    print_header "Release Complete"
    echo -e "${GREEN}Release $VERSION successfully created and deployed!${NC}"
    echo ""
    echo "Release Location: $PROJECT_ROOT/release/goyolopenvino-$VERSION"
    echo "Archive: $PROJECT_ROOT/release/goyolopenvino-$VERSION.tar.gz"
    echo "Install Path: $INSTALL_PATH"
    echo ""
    echo "Next steps:"
    echo "  1. Start service: sudo systemctl start goyolo"
    echo "  2. Enable auto-start: sudo systemctl enable goyolo"
    echo "  3. Check status: sudo systemctl status goyolo"
}

# Show usage
show_usage() {
    cat << EOF
${GREEN}=== GoYOLO All-in-One Release Script ===${NC}

Usage: $0 [VERSION] [MODEL] [INSTALL_PATH]

Arguments:
  VERSION       Release version (default: v1.0.0)
  MODEL         YOLO model type (default: yolov8n)
  INSTALL_PATH  Installation path (default: /opt/goyolo)

Supported Models:
  - yolov8n, yolov8s, yolov8m, yolov8l
  - yolov11n, yolov11s, yolov11m

Examples:
  # Default release
  $0

  # Custom version
  $0 v2.0.0

  # Custom version and model
  $0 v2.0.0 yolov11n

  # Custom everything
  $0 v2.0.0 yolov8s /home/user/goyolo

Workflow:
  1. Create release package
  2. Download and convert models
  3. Package release archive
  4. Verify archive integrity
  5. Deploy to system

EOF
}

# Main entry point
if [[ "$1" == "-h" ]] || [[ "$1" == "--help" ]]; then
    show_usage
    exit 0
fi

main

